package DAO.MySQL;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import DAO.RankDAO;
import Model.RankBean;

public class RankDAOImpl
	extends GenericDAO<RankBean>
	implements RankDAO
{
	private final String TARGET_TABLE = "rank_tb";
	
	@Override
	protected RankBean parseResultSet(ResultSet rs) throws SQLException {
		RankBean rankBean = new RankBean();
		rankBean.setRank_id( (rs.getInt("rank_id") ) );
		rankBean.setRank_name( (rs.getString("rank_name") ) );
		rankBean.setTotal_days( (rs.getInt("total_days") ) );
		return rankBean;
	}
	
	@Override
	public ArrayList<RankBean> findAllRank() 
		throws SQLException, ClassNotFoundException
	{
		String query = "select * from " + TARGET_TABLE;
		return (executeMultipleQuery(query) );
	}

	@Override
	public RankBean findByRankID(int input)
		throws SQLException, ClassNotFoundException
	{
		String query = "select * from " + TARGET_TABLE + " where rank_id='" + input + "'";
		ArrayList<RankBean> tempList = executeMultipleQuery(query);
		if (tempList.size() == 1)
		{ return tempList.get(0); }
		else 
		{ return null; }
	}
	
	@Override
	public int findNextRank()
		throws SQLException, ClassNotFoundException {
		int output = 1;
		String query = "select max(rank_id) from " + TARGET_TABLE;
		String temp = executeOneCellQuery(query);
		if (temp == null) {	//If no results found
			output = 1;			//return 1
		} else {					//Otherwise results found
			output = ( Integer.parseInt(temp) + 1 );
		}
		return output;
	}
	
	@Override
	public void addRank(RankBean inRank)
		throws SQLException, ClassNotFoundException
	{
		//insert into rank_tb values (3,'Janitor',10);
		StringBuffer statement = new StringBuffer();
		statement.append("insert into " + TARGET_TABLE + " value (");
		statement.append( inRank.getRank_id() + ",");
		statement.append( "'" + inRank.getRank_name() + "',");
		statement.append( inRank.getTotal_days() );
		statement.append(")");
		//System.out.println(statement);
		executeSingleUpdate( statement.toString() );
	}
	
	@Override
	public void updateRank(RankBean inRank)
		throws SQLException, ClassNotFoundException
	{
		//update rank_tb set rank_name='cleaner',total_days=20 where rank_id=3;
		StringBuffer statement = new StringBuffer();
		statement.append("update " + TARGET_TABLE + " set ");
		statement.append("rank_name='" + inRank.getRank_name() + "' ,");
		statement.append("total_days='" + inRank.getTotal_days() + "' ");
		statement.append("where rank_id='" + inRank.getRank_id() + "'");
		//System.out.println(statement);
		executeSingleUpdate( statement.toString() );
		
	}
	
	@Override
	public void deleteRank(RankBean inRank)
		throws SQLException, ClassNotFoundException
	{	//Pass to delete by Integer ID
		deleteRank( inRank.getRank_id() );
	}
	
	@Override
	public void deleteRank(int inRankID)
		throws SQLException, ClassNotFoundException
	{
		//delete from rank_tb where rank_id=3;
		StringBuffer statement = new StringBuffer();
		statement.append("delete from " + TARGET_TABLE + " where rank_id=" + inRankID);
		//System.out.println(statement);
		executeSingleUpdate( statement.toString() );		
	}
}
